#define _CRT_SECURE_NO_WARNINGS 1

//练习1
//数组类型

//编写代码,演示多个字符从两端移动，向中间汇聚

//welcome to bit!!!!!

#include <stdio.h>
#include <string.h>
#include <windows.h>

int main()
{
	char arr1[] = "welcome to bit!!!!!";
	char arr2[] = "*******************";
	int left = 0;
	int right = strlen(arr1)-1;
	
	while (left <= right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);//休眠,单位是毫秒 //头文件是windows.h
		system("cls");//清理

		left++;
		right--;

	}


	return 0;
}

//练习2
//二分查找
//在有序的数组中，查找指定的数值
//升序
//[1 2 3 4 5 6 7 8 9 10]

//普通查找
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 7;
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0;i < sz;i++)
	{
		if (arr[i] == k)
		{
			printf("找到了，下标是:%d\n", i);
			break;
		}
	}
	if (i == sz)
		printf("找不到\n");

	return 0;
}


//折半查找(二分查找)
// 速度快，但是条件苛刻，有序数组才能二分查找
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%d", &k);
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);

	int left = 0;
	int right = sz - 1;


	while (left <= right)
	{
		int mid = (left + right) / 2; //中间元素的下标

		if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else
		{
			printf("知道了，下标是:%d\n", mid);
			break;
		}
	}
	if (left > right)
		printf("找不到了\n");

	return 0;
}

//或者
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%d", &k);
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);

	int left = 0;
	int right = sz - 1;

	int flag = 0; //假设找不到
	while (left <= right)
	{
		int mid = (left + right) / 2; //中间元素的下标

		if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else
		{
			printf("知道了，下标是:%d\n", mid);
			flag = 1; //找到了
			break;
		}
	}
	if (flag == 0)
		printf("找不到了\n");

	return 0;
}

/*二分查找可能存在的问题
int mid = (left + right) / 2;
如果left和right的值特别大就存在问题
*/
//如：
int main()
{
	int left = 2147483646;
	int right = 2147483646;
	int mid = (left + right) / 2;

	printf("%d\n", mid);//  -2
	return 0;
}
//修改
int main()
{
	int left = 2147483646;
	int right = 2147483646;
	int mid = left + (right - left) / 2;

	printf("%d\n", mid);//
	return 0;
}